package com.observer;

import java.util.Observable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author: sun.hongliang
 * @create: 2019/10/18 12:21
 */
public class ServiceThread extends Observable implements Runnable {

    private volatile int numOne;

    private volatile int numTwo;

    public ServiceThread(int numOne, int numTwo) {
        this.numOne = numOne;
        this.numTwo = numTwo;
    }

    public void run() {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future future = executor.submit(new AccountTask(numOne, numTwo));
        try {
            String result = (String) future.get();
            int sum = Integer.parseInt(result);

            Account obj = new Account();
            obj.setNumOne(this.numOne);
            obj.setNumTwo(this.numTwo);
            obj.setSum(sum);
            setChanged();
            notifyObservers(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        executor.shutdown();
    }
}
